/**
 * Panel de chargement
 * 
 * @author Marie-Hélène Goddet, Damien Finck, Cédric Schaller
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

import utils.Resources;

@SuppressWarnings("serial")
public class JLoad extends JPanel {
	
	private ActionListener actionListener;
	Timer minuteur;
	
	protected JPanelBackground jp_load;
		protected JProgressBar jpb_load;
	
	/**
	 * Constructeur
	 */
	public JLoad() {
		/** Evénements **/
		ActionListener action = new Event_JPLoad(this);
		/** Création des éléments     **/
		this.jp_load = new JPanelBackground(null,Resources.getImage("images/load.png",getClass()));
			this.jpb_load = new JProgressBar();
		/** Paramétrage des éléments  **/
		this.jpb_load.setVisible(true);
		this.jpb_load.setBounds(500,420,250,20);
		this.setLayout(new BorderLayout());
		/** Action sur les éléments   **/
		/** Montage des éléments      **/
		this.add(this.jp_load,BorderLayout.CENTER);
			this.jp_load.add(this.jpb_load);
		/** Minuteur **/
		minuteur = new Timer(500,action);
		minuteur.start();
	}
	public void addActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}
	public ActionListener getActionListener() {
		return actionListener;
	}
}
class Event_JPLoad implements ActionListener {
	JLoad jp;
	/**
	 * Constructeur
	 * @param jp le panel de chargement de l'application
	 */
	public Event_JPLoad(JLoad jp) {
		this.jp=jp;
	}
	/**
	 * Appelé à la fin du timer
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object obj = arg0.getSource();
		if (obj instanceof Timer) {
			if (jp.jpb_load.getValue()==100) {
				jp.minuteur.stop();
				
				ActionEvent e = new ActionEvent(jp,1001,"JPLoad_done");
				jp.getActionListener().actionPerformed(e);
				
			} else if (jp.jpb_load.getValue()==0) {
				jp.jpb_load.setVisible(true);
				jp.jpb_load.setValue(1);
				jp.minuteur.setDelay(10);
			} else {
				//Barre de chargement +1
				jp.jpb_load.setValue(jp.jpb_load.getValue()+1);
			}
		}
	}
}